<?php

require_once ROOT_DIR.'models/User.php';
require_once ROOT_DIR.'models/Todo.php';
require_once ROOT_DIR.'models/Message.php';

/**
 * @author Immanuel Bauer | 0626117
 */
class TodoListJSON {
	
	/**
	 * Generates the JSON view for the given TodoList model object or array of TodoList model objects.
	 * @param mixed $todoList
	 * @param boolean $fullTodos
	 * @param int $count
	 * @param boolean $compact
	 * @return string
	 */
	public static function getView($todoList, $fullTodos = false, $count = null, $compact = false){
		
		if(is_array($todoList)){
			$field = 'lists';
			$a = array();
			
			$lc = count($todoList);
			for($c = 0; $c < $lc; $c++){
				array_push($a, TodoListJSON::buildTodoListArray($todoList[$c], $fullTodos, $compact));
			}
		} else {
			$field = 'list';
			$a = TodoListJSON::buildTodoListArray($todoList, $fullTodos, $compact);
			$su = User::getUsersLinkedToList($todoList->getID());
			$uc = count($su);
			$sua = array();
		
			for($c = 0; $c < $uc; $c++){
				$user = $su[$c];
				$u = array(
						'id' => $user->getID(),
						'nick' => $user->getLoginName(),
						'fN' => $user->getFirstName(),
						'lN' => $user->getLastName());
				array_push($sua, $u);
			}
			$a['sU'] = $sua;
		}
		
		$res = new ResultJSON(false, session_id());
		$res->addItem($field, $a);
		if(!is_null($count)) $res->addItem('lC', $count);
		
		return $res->renderView();
	}
	
	/**
	 * Builds the array for the given TodoList
	 * @param TodoList $todoList
	 * @param boolean $fullTodos
	 */
	private static function buildTodoListArray($todoList, $fullTodos = false, $compact = false){
		if(!$compact){
			$owner = User::getUser($todoList->getOwnerID());
			$a = array(
					'id' => $todoList->getID(),
					'title' => $todoList->getName(),
					'text' => $todoList->getDescription(),
					'owner' => array(
						'id' => $owner->getID(),
						'nick' => $owner->getLoginName(),
						'fN' => $owner->getFirstName(),
						'lN' => $owner->getLastName())
				);
			
			$ta = TodoListJSON::buildTodoArray($todoList, $fullTodos);
			$a['tC'] = count($ta);
			$a['mC'] = Message::getMessageCount('','',$todoList->getID());
			$a['todos'] = $ta;
		} else {
			$a = array(
					'id' => $todoList->getID(),
					'title' => $todoList->getName(),
					'oID' => $todoList->getOwnerID()
				);
		}
		
		if($todoList->isPublic()) $a['public'] = true;
		
		return $a;
	}
	
	/**
	 * Generates the list of todos for the given TodoList
	 * @param TodoList $todoList
	 */
	private static function buildTodoArray($todoList, $fullTodos = false){
		$todos = Todo::getTodosInList($todoList->getID());
		$tc = count($todos);
		
		$ta = array();
		
		for($c = 0; $c < $tc; $c++){
			$todo = $todos[$c];
			if($fullTodos){
				$t = array(
					'id' => $todo->getID(),
					'title' => $todo->getTitle(),
					'text' => $todo->getDescription(),
					'tD' => $todo->getTargetDate(),
					'pH' => $todo->getProjectedHours(),
					'status' => $todo->getStatus(),
					'creator' => $todo->getCreatorID()
				);
				
				array_push($ta, $t);
			} else {
				array_push($ta, $todo->getID());
			}
		}
		
		return $ta;
	}
}

?>